﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FmcWebsite.Core
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Thumbnail of an image.
    /// </summary>
    public class Thumbnailer
    {
        const int default_size = 80;
        Bitmap bmp;

        /// <summary>
        /// Creates a thumbnail from an image file with a 
        /// standard size: 80x80 pixles.        
        /// </summary>
        /// <param name="path">path to the source image file</param>
        public Thumbnailer(string path)
        {
            bmp = CreateThumbnail(Image.FromFile(path), new Size(default_size, default_size));
        }

        /// <summary>
        /// Creates a thumbnail from an image file with a custom size.
        /// </summary>
        /// <param name="path">Path to the source image file</param>
        /// <param name="size">The custom size of the thumbnail.</param>
        public Thumbnailer(string path, Size size)
        {
            bmp = CreateThumbnail(Image.FromFile(path), size);
        }

        /// <summary>
        /// Creates a thumbnail from an image file with a custom long side size.
        /// The short side of the thumbnail will be in proportion of the source.
        /// </summary>
        /// <param name="path">The source image file.</param>
        /// <param name="long_side">The thumbnail long side length in pixel. 
        /// </param>
        public Thumbnailer(string path, int long_side)
        {
            bmp = CreateThumbnail(Image.FromFile(path), long_side);
        }


        /// <summary>
        /// Creates a thumbnail bitmap with a custom size.
        /// </summary>
        /// <param name="source">the image source</param>
        /// <param name="size">the thumbnail size</param>
        /// <returns></returns>
        static public Bitmap CreateThumbnail(Image source, Size size)
        {
            Bitmap bmp = new Bitmap(size.Width, size.Height);
            Graphics graph = Graphics.FromImage(bmp);
            Rectangle cropsize = GetSourceCropArea(source.Size, bmp.Size);

            graph.DrawImage(
                source,
                new Rectangle(0, 0, bmp.Width, bmp.Height),
                cropsize,
                GraphicsUnit.Pixel);

            return bmp;
        }

        /// <summary>
        /// Creates a thumbnail bitmap with a custom long side size.
        /// The short side will be in proportion of the source image.
        /// </summary>
        /// <param name="source">the source image</param>
        /// <param name="long_side">customized long side of the thumbnail in pixels</param>
        /// <returns></returns>
        static public Bitmap CreateThumbnail(Image source, int long_side)
        {
            Size size = new Size(long_side, long_side);
            if (source.Width > source.Height)
            {
                size.Height = (size.Width * source.Height) / source.Width;
            }

            if (source.Width < source.Height)
            {
                size.Width = (size.Height * source.Width) / source.Height;
            }

            return CreateThumbnail(source, size);
        }

        static private Rectangle GetSourceCropArea(Size source, Size target)
        {
            Size crop_size;
            Point origin;

            // try to maximize the width
            crop_size = new Size(source.Width, (source.Width * target.Height) / target.Width);
            origin = new Point(0, (source.Height - crop_size.Height) / 2);

            // try to maximize the height
            if (crop_size.Height > source.Height)
            {
                crop_size = new Size((source.Height * target.Width) / target.Height, source.Height);
                origin = new Point((source.Width - crop_size.Width) / 2, 0);
            }

            return new Rectangle(origin, crop_size);
        }

        /// <summary>
        /// Gets the bitmap of the thumbnail.
        /// </summary>
        public Bitmap Bitmap
        {
            get { return bmp; }
        }

        /// <summary>
        /// Draw the thumbnail in the middle of the area on the 
        /// given graphics.
        /// </summary>
        /// <param name="graphics">Graphics object where to draw the thumbnail.</param>
        /// <param name="area">The thumbnail will be drawn in the center of this area.</param>
        public void DrawCenter(Graphics graphics, Rectangle area)
        {
            Point origin = new Point(
                                (area.Width - bmp.Width) / 2,
                                (area.Height - bmp.Height) / 2);
            graphics.DrawImage(bmp, origin);
        }
    }
}
